<?php
/**
 * @Author: anchen
 * @Date:   2016-03-11 15:36:08
 * @Last Modified by:   anchen
 * @Last Modified time: 2016-03-14 09:33:55
 */
namespace Home\Controller;
use Think\Controller\RestController;
class LoginController extends RestController {
    public function index(){
        $this->display();
    }
    public function login(){
        header("Content-Type: text/html; charset=utf-8");
        switch ($this->_method) {
            case 'get':
                echo "1";
                break;
            case 'post':
                /**
                 * 登录判断
                 * @access public
                 * @param string username 用户名
                 * @param string password 密码
                 * @return 成功：跳转
                 * @return 失败：返回
                 */
                //登陆处理页面
                $username = I ( "post.user", "", "trim" );
                $password = I ( "post.pwd", "", "trim" );
                $ip=$_SERVER["SERVER_ADDR"];    
                $member = M('member');
                $res = $member->where("username = '".$username."'")->find(); 
                if ($res) {
                    if ($password==$res['password']) {
                        //登录成功
                        echo "登录成功".$res['password'].$password;
                        $data['login_status']=1;
                        $data['login_pwd']="成功登录";
                    }else{
                        //密码错误
                        echo "密码错误".$res['password'].$password;
                        $data['login_status']=2;
                        $data['login_pwd']=$password;
                    }
                }else{
                    //用户名不存在
                    echo "无此用户";
                    $data['login_status']=3;
                    $data['login_pwd']=$password;
                }
                /**
                 * 记录登录日志
                 * @access public
                 * @param string $data 参数
                 */
                //登陆处理页面
                $data['login_name']=$username;
                $data['login_datatime']=time();
                $data['login_ip']=$_SERVER["SERVER_ADDR"];
                $loginmessage=M('loginmessage');
                $login=$loginmessage->add($data);
                if($data['login_status']==1){
                       $this->success("登录成功","/test/3.11/database/index.php#/");
                    }else{
                       $this->error("用户名或密码错误");
                       exit;
                    }

            
                
                

                break;
            case 'put':
                E(C('CODE.METHOD_NOT_ALLOWED')['msg'], $status = C('CODE.METHOD_NOT_ALLOWED')['code']);
                break;
            case 'delete':
                E(C('CODE.METHOD_NOT_ALLOWED')['msg'], $status = C('CODE.METHOD_NOT_ALLOWED')['code']);
                break;
            case 'head':
                break;
            case 'options':
                return $this->options('Access-Control-Allow-Methods: GET, POST, OPTIONS');
                break;
            default:
                E(C('CODE.METHOD_NOT_ALLOWED')['msg'], $status = C('CODE.METHOD_NOT_ALLOWED')['code']);
        }
    }
}